git commands

last modified

2023–9–5

clone
create local repository as copy of remote one (“origin”)
the branches of the local repository track the branches of origin
push
upload local changes of the repository to a remote repository,
if they only consist of additional commits
fetch
download remote changes of a remote repository to the local one,
if they only consist of additional commits
pull
like fetch, but additionally tries to merge (typically fast-forward)
rebase
apply patch
merge
uses recursive three-way merge

start from clone, push and pull

GitHub recommends to always use HTTPS repository URLs.

git clone https://github.com/<user/repository>.git
git push
git push -f
git pull

start from scratch

git init
git remote add origin
git push -u origin master https://github.com/<user/repository>.git

stage and commit

git add
git commit -a -m "commit message"
git commit -a -m "commit message" --amend
echo "repository description" > .git/description

status, log, and diff

git status
git diff
git diff *commit*
git log
git difftool -d
git difftool -d *commit*
meld . &

checkout

git checkout
git checkout master
git checkout -f

change files such that git records it

git mv <oldfilename> <newfilename>
git rm <file>

tag

use only lightweight tags

git tag <tag> <commit>

push them to GitHub

git push --tags

then use them to create releases (they retain the date of the commit)
or create releases on GitHub, along with them automatically lightweight tags are created, pull them using git pull

remove file from repository

https://help.github.com/articles/remove-sensitive-data/

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA>' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all

commit references

A commit may be referred to by its hash or by a reference (including lightweight tags). Standard references are:

HEAD
tip of current branch
master
default name for the tip of the “main” branch
origin
default name for a remote repository, refers by default to origin/master

suffix: ~ parent, ~<n> nth parent

global configuration

Global configuration is stored in ~/.gitconfig.

git config --global core.editor "pulsar -n -w"
git config --global diff.tool meld

Make sure that the Debian package meld is installed.

git-credential-libsecret

There is a git credentials helper included with git, but only as source code. Install libsecret-1-dev. As root:

/usr/share/doc/git/contrib/credential/libsecret
make
mv git-credential-libsecret /usr/local/bin/
make clean

This creates the executable git-credential-libsecret and makes it accessible. Configure it:

git config --global credential.helper /usr/local/bin/git-credential-libsecret

for GitHub

git config --global user.name "<name>"
git config --global user.email "<email>"

The email address should be associated with the GitHub account. The setting Access / Emails ‘Keep my email address private’ should be active.

When prompted for a password, do not use the account password but a personal access token, see Settings / Developer Settings / Personal access tokens.